Skip to content

Add asc keyword to sort command#4113

Merged
Swiddis merged 8 commits intoopensearch-project:mainfrom
ritvibhatt:add-asc-to-sort
Sep 2, 2025
Merged

Add asc keyword to sort command#4113
Swiddis merged 8 commits intoopensearch-project:mainfrom
ritvibhatt:add-asc-to-sort

Conversation

@ritvibhatt
Copy link
Contributor

Description

  • Adds asc/a keyword as option in sort command, Specifying asc does not change behavior, sort direction of each field remains as specified (ascending as default)
  • Adds missing tests to CaclitePPlSortIT for desc keyword, count, and type casting enhancements

Changes

  • Added ASC and A tokens to PPL lexer and parser grammar
  • Updated sort command to accept asc/a keywords alongside existing desc/d

Related Issues

Resolves #[Issue number to be closed when this PR is merged]

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • New PPL command checklist all confirmed.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff or -s.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Copy link
Collaborator

@Swiddis Swiddis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs doc, otherwise LGTM

Swiddis
Swiddis previously approved these changes Aug 26, 2025
Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
Copy link
Collaborator

@RyanL1997 RyanL1997 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ritvibhatt, thanks for taking this on, and i just left some comments.

* [+|-]: optional. The plus [+] stands for ascending order and NULL/MISSING first and a minus [-] stands for descending order and NULL/MISSING last. **Default:** ascending order and NULL/MISSING first.
* sort-field: mandatory. The field used to sort. Can use ``auto(field)``, ``str(field)``, ``ip(field)``, or ``num(field)`` to specify how to interpret field values.
* [desc|d]: optional. Reverses the sort results. If multiple fields are specified, reverses order of the first field then for all duplicate values of the first field, reverses the order of the values of the second field and so on.
* [asc|a|desc|d] (Since 3.3): optional. asc/a keeps the sort order as specified. desc/d reverses the sort results. If multiple fields are specified with desc/d, reverses order of the first field then for all duplicate values of the first field, reverses the order of the values of the second field and so on. **Default:** asc.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider rewording to 'sorts in ascending order' for clarity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior for asc is it will keep whatever the specified field sort order was, so for sort firstname, -lastname asc firstname will still be sorted ascending and lastname will still be sorted descending. Don't know if 'sorts in ascending order' would make that behavior clear



* count: optional. The number of results to return. **Default:** returns all results. Specifying a count of 0 or less than 0 also returns all results.
* count (Since 3.3): optional. The number of results to return. **Default:** returns all results. Specifying a count of 0 or less than 0 also returns all results.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have these covered in example section?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! just added 'Since 3.3' in this PR but added examples in previous PR


sortCommand
: SORT (count = integerLiteral)? sortbyClause (DESC | D)?
: SORT (count = integerLiteral)? sortbyClause (ASC | A | DESC | D)?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider the precedence if someone accidentally specifies both asc and desc? The grammar allows it but behavior might be undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked using both asc and desc will result in an error

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it. Thanks for confirming that.

}

@Test
public void testSortWithAscKeyword() throws IOException {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider adding negative test cases:

  • Invalid combinations like 'sort field asc desc'
  • Edge cases with count=0"

@Swiddis Swiddis merged commit 6db8d80 into opensearch-project:main Sep 2, 2025
23 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Sep 2, 2025
* add asc keyword to sort

Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>

* add tests and update docs

Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>

* remove a

Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>

* add a keyword

Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>

* update doc

Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>

* empty

Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>

* fix formatting

Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>

* empty

Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>

---------

Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
(cherry picked from commit 6db8d80)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
penghuo pushed a commit that referenced this pull request Sep 3, 2025
* add asc keyword to sort



* add tests and update docs



* remove a



* add a keyword



* update doc



* empty



* fix formatting



* empty



---------


(cherry picked from commit 6db8d80)

Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 2.19-dev enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants